package com.fare.taluo.core.domain.thread;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.fare.taluo.conf.ConfigContainer;
import com.fare.taluo.conf.PropertyUtils;
import com.fare.taluo.core.domain.config.city.PortEntity;
import com.fare.taluo.core.domain.config.line.FlightInfoEntity;
import com.fare.taluo.core.domain.crawl.CrawlCSAirLineModel;
import com.fare.taluo.core.domain.crawl.CrawlPropertiesLoader;
import com.fare.taluo.core.domain.crawl.adapter.sub.MFAirLineAdaptor;
import com.fare.taluo.vo.core.FlightInfoVO;

public class MFAirLineThread extends Thread  {
	private List<String> portCodes ;
	
	
	public MFAirLineThread(List<String> portCodes) {
		this.portCodes = portCodes;
	}
	
	@Override
	public void run() {
		//1获得所有机场信息
		int count = 0,sum=0;
		
		/*
		 * //加载配置文件
		//系统默认的config.properties配置文件
		ConfigContainer.getInstance().register(DefaultPropertiesLoader.class);
		//hbase数据库配置文件(可以改为像jdbc.properties文件一样由spring加载)
		ConfigContainer.getInstance().register(HBasePropertiesLoader.class);
		//抓取数据相关配置
		 */		
		ConfigContainer.getInstance().register("com.fare.taluo.core.domain.crawl.CrawlPropertiesLoader");
		
		PortEntity portEntity = new PortEntity();
		FlightInfoEntity entity = new FlightInfoEntity();
		List<PortEntity> portList =  portEntity.findAll();//获得所有机场
		Map<String,PortEntity> map = portEntity.findPortEntityToMapByName();//以map的形式保存机场 key:机场中文名称
		//3循环去访问网站尝试
		for (Iterator iterator = portCodes.iterator(); iterator.hasNext();) {
			/*String string = (String) iterator.next();
			String[] codes = string.split("-");
			String fromCityCode = codes[0];
			String toCityCode = codes[1];*/
			String fromCityCode=(String)iterator.next();
			//抓取机场模型
			CrawlCSAirLineModel portModel = new CrawlCSAirLineModel();
			portModel.setFromCity(fromCityCode);
//			portModel.setToCity(toCityCode);
			MFAirLineAdaptor adap = new MFAirLineAdaptor(portModel);
			List<Object>  obj = null;
			int fetchCount = PropertyUtils.getIntProperty("crawl.line.fail.repeat.times", CrawlPropertiesLoader.CONFIG_FILE);
			try{
				while(fetchCount > 0){
				obj = adap.fetch(null);
			    fetchCount--;	
				    if(obj.size()>0){
				    	System.out.println("----running---- "+this.getName()+" "+(++sum));
				    	//循环执行，存入数据库中
				    	for (Iterator iterator2 = obj.iterator(); iterator2.hasNext();){
				    		FlightInfoVO vo=(FlightInfoVO) iterator2.next();
				    		entity.setEntryTime(new Date());
				    		entity.setCompanyCode("MF");
				    		entity.setCompanyName("厦门航空");
				    		entity.setFromCityName(vo.getFromCityName());
				    		entity.setToCityName(vo.getToCityName());
				    		if(vo.getStopCity() !=null && vo.getStopCity().length()>0){
				    			entity.setStopCity(vo.getStopCity());
					    		
				    		}
				    		entity.setEffectEndTime(vo.getEffectEndTime());
				    		entity.setEffectStartTime(vo.getEffectStartTime());
				    		entity.setFlightNo(vo.getFlightNo());				    		
				    		entity.setToCity(map.get(vo.getToCityName()).getCode());
				    		entity.setFromCity(map.get(vo.getFromCityName()).getCode());
				    		entity.setFlightStartTime(vo.getFlightStartTime());
				    		entity.setFlightEndTime(vo.getFlightEndTime());
				    		entity.setFrequence(vo.getFrequence());
				    		
				    		entity.tranSave(entity);
				    		
				    		System.out.println("+++保存++++++++++++++++++++++++++++++++++");
				    	}
				    	
				    	
				    }
				}
			}catch (Exception e) {
				try {
					throw new Exception("保存数据时出错");
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}		
		System.out.println("---------finished---------");
		ThreadFlag.setIsFinish(true);
	}

	public boolean validate(Object obj){
		if(null!= obj && !obj.toString().contains("您查询的航班不存在")){
			return true;
		}else{
			return false;
		}
		
	}
}
